package com.pda.xinmazhi.utils;

import android.text.TextUtils;
import android.util.Log;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;


public class DateFormatUtil {
    private static SimpleDateFormat sf = null;
    private static final String TAG = "DateFormat";

    //时间戳转换成字符串
    public static String getDateToString(long time) {
        String s = "";
        if (time != 0d) {
            Date d = new Date(time);
            sf = new SimpleDateFormat("yyyy.MM.dd");
            s = sf.format(d);
        }
        return s;

    }

    public static String getDateToString(Date date) {
        sf = new SimpleDateFormat("yyyy-MM-dd");
        return sf.format(date);
    }

    public static String getDateToStringForMedical(Date date) {
        sf = new SimpleDateFormat("yyyyMMdd");
        return sf.format(date);
    }

   /* public static String getDateToStringForMedicalOnlyMonth(Date date) {
        sf = new SimpleDateFormat("yyyyMM");
        return sf.format(date) + "00";
    }*/

    public static String getDateToStringDay(long time) {
        if (time == 0d) {
            return "";
        } else {
            Date d = new Date(time);
            sf = new SimpleDateFormat("yyyy-MM-dd");
            return sf.format(d);
        }
    }

    public static String getDateToStringDay(Date date) {
        sf = new SimpleDateFormat("yyyy-MM-dd");
        return sf.format(date);
    }
    public static String getDateToStringMonth(Date date) {
        sf = new SimpleDateFormat("yyyy-MM");
        return sf.format(date);
    }
    public static Long getDateStringToLong(String dateString) {
        Log.d(TAG, "dateString is " + dateString);
        if (TextUtils.isEmpty(dateString) || dateString == null)
            return System.currentTimeMillis();

        sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        try {
            date = sf.parse(dateString);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date.getTime();
    }

    public static String getDateToStringLong(Date date) {
        sf = new SimpleDateFormat("yyyy-MM-dd HH:mm");

        return sf.format(date);
    }


    //说明：此方法的存在是因为之前写的代码导致在本地存储的时间相关不规范  有的存储下来的是时间戳 有的是存储下来的字符串
    //判断当前字符串位数  如果是13位  说明之前存储的是时间戳
    public static Date getDate(String dateString) {
      /*  Date date = null;
        sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
        date = sf.parse(dateString);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;   */
        Date date = null;
        if (dateString.length() == 13) {
            date = new Date(Long.valueOf(dateString));
        } else {
            sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            try {
                date = sf.parse(dateString);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        return date;
    }


    public static String secToTime(int time) {
        String timeStr = null;
        int hour = 0;
        int minute = 0;
        int second = 0;
        if (time <= 0)
            return "00:00";
        else {
            minute = time / 60;
            if (minute < 60) {
                second = time % 60;
                timeStr = unitFormat(minute) + ":" + unitFormat(second);
            } else {
                hour = minute / 60;
                if (hour > 99)
                    return "99:59:59";
                minute = minute % 60;
                second = time - hour * 3600 - minute * 60;
                timeStr = unitFormat(hour) + ":" + unitFormat(minute) + ":" + unitFormat(second);
            }
        }
        return timeStr;
    }

    public static String unitFormat(int i) {
        String retStr = null;
        if (i >= 0 && i < 10)
            retStr = "0" + Integer.toString(i);
        else
            retStr = "" + i;
        return retStr;
    }


}
